package br.tiagohm.markdownview.app.slice;

import br.tiagohm.markdownview.MarkdownView;
import br.tiagohm.markdownview.app.ResourceTable;
import br.tiagohm.markdownview.css.InternalStyleSheet;
import br.tiagohm.markdownview.css.styles.Github;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.PopupDialog;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class MainAbilitySlice extends AbilitySlice {

    private MarkdownView mMarkdownView;
    private InternalStyleSheet mStyle = new Github();

    private Image mark_img;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor("#313A99"));

        //Cria o bean.
        MyBean myBean = new MyBean();
        myBean.setHello("Olá");
        myBean.setDiasDaSemana(MyBean.DiasDaSemana.DOMINGO);

        mMarkdownView = (MarkdownView) findComponentById(ResourceTable.Id_mark_view);
        mMarkdownView.addStyleSheet(mStyle);
        //http://stackoverflow.com/questions/6370690/media-queries-how-to-target-desktop-tablet-and-mobile
        mStyle.addMedia("screen and (min-width: 320px)");
        mStyle.addRule("h1", "color: green");
        mStyle.endMedia();
        mStyle.addMedia("screen and (min-width: 481px)");
        mStyle.addRule("h1", "color: red");
        mStyle.endMedia();
        mStyle.addMedia("screen and (min-width: 641px)");
        mStyle.addRule("h1", "color: blue");
        mStyle.endMedia();
        mStyle.addMedia("screen and (min-width: 961px)");
        mStyle.addRule("h1", "color: yellow");
        mStyle.endMedia();
        mStyle.addMedia("screen and (min-width: 1025px)");
        mStyle.addRule("h1", "color: gray");
        mStyle.endMedia();
        mStyle.addMedia("screen and (min-width: 1281px)");
        mStyle.addRule("h1", "color: orange");
        mStyle.endMedia();
        mMarkdownView.setBean(myBean);
        mMarkdownView.loadMarkdownFromAsset("markdown1.md");

        mark_img = (Image) findComponentById(ResourceTable.Id_mark_img);

        mark_img.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                PopupDialog popupDialog = new PopupDialog(getContext(), mark_img, 450, 100);
                Component view = LayoutScatter.getInstance(getContext())
                        .parse(ResourceTable.Layout_ability_main_txt, null, false);
                view.setLayoutConfig(new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT));
                popupDialog.setCustomComponent(view);
                popupDialog.setCornerRadius(15F);
                popupDialog.setTransparent(true);
                popupDialog.setOffset(720,390);

                popupDialog.setDialogListener(new BaseDialog.DialogListener() {
                    @Override
                    public boolean isTouchOutside() {
                        popupDialog.destroy();
                        return true;
                    }
                });
                popupDialog.show();
                runUI(new Runnable() {
                    @Override
                    public void run() {
                        if(popupDialog.isShowing())
                            popupDialog.destroy();
                    }
                });
            }
        });

    }

    public static void runUI(Runnable runnable){
        // 切换到主线程
        EventRunner runner = EventRunner.getMainEventRunner();
        EventHandler eventHandler = new EventHandler(runner);
        //切换任务
        eventHandler.postTask(runnable,2500);
    }

    public static class MyBean {

        public enum DiasDaSemana {
            DOMINGO,
            SEGUNDA,
            TERCA,
            QUARTA,
            QUINTA,
            SEXTA,
            SABADO
        }

        private String hello;
        private DiasDaSemana diasDaSemana;

        public String getHello() {
            return hello;
        }

        public void setHello(String hello) {
            this.hello = hello;
        }

        public DiasDaSemana getDiasDaSemana() {
            return diasDaSemana;
        }

        public void setDiasDaSemana(DiasDaSemana diasDaSemana) {
            this.diasDaSemana = diasDaSemana;
        }
    }

}





